package com.jake.sap.aspect;

import com.jake.common.constant.SizeConstants;
import com.jake.common.util.ThreadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Queue;

@Component
@Slf4j
public class ConnectionReleaseService {

    @Autowired
    @Qualifier(value = "sapConcurrentQueue")
    private Queue<LocalDateTime> sapConcurrentQueue;

    @Async
    public void releaseLatest() {
        ThreadUtils.sleep(300);
        if (sapConcurrentQueue.size() > SizeConstants.SAP_THRESHOLD_SIZE) {
            LocalDateTime removedTime = sapConcurrentQueue.poll();
            log.warn("release: the current size is {}, the removed element is {}", sapConcurrentQueue.size(), removedTime);
        }
    }

}
